//
//  FRPPhotoViewModel.m
//  FunctionalReactivePixels
//
//  Created by sun on 15/7/16.
//  Copyright (c) 2015年 sunfei. All rights reserved.
//

#import "FRPPhotoViewModel.h"
#import "FRPPhotoImporter.h"
#import "FRPPhotoModel.h"

@interface FRPPhotoViewModel ()

@property (nonatomic, strong) UIImage *photoImage;
@property (nonatomic, getter = isLoading) BOOL loading;

@end

@implementation FRPPhotoViewModel

- (instancetype)initWithModel:(FRPPhotoModel *)photoModel {
    self = [super initWithModel:photoModel];
    if (self) {
        @weakify(self)
        [self.didBecomeActiveSignal subscribeNext:^(id x) {
            @strongify(self)
            self.loading = YES;
            [[FRPPhotoImporter fetchPhotoDetails:self.model] subscribeError:^(NSError *error) {
                NSLog(@"could not fetch photo details:%@", error);
            } completed:^{
                self.loading = NO;
                NSLog(@"fetched photo details");
            }];
        }];
        
        RAC(self, photoImage) = [RACObserve(self.model, fullsizedData) map:^id(id value) {
            return [UIImage imageWithData:value];
        }];
    }
    return self;
}

- (NSString *)photoName {
    return self.model.photoName;
}

@end
